package com.changgou.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.order.pojo.Task;
import com.changgou.user.dao.*;
import com.changgou.user.pojo.*;
import com.changgou.user.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PointLogMapper pointLogMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private ProvincesMapper provincesMapper;

    @Autowired
    private CitiesMapper citiesMapper;

    @Autowired
    private AreasMapper areasMapper;

    /**
     * 查询全部列表
     *
     * @return
     */
    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    /**
     * 根据ID查询
     *
     * @param username
     * @return
     */
    @Override
    public User findById(String username) {
        return userMapper.selectByPrimaryKey(username);
    }


    /**
     * 增加
     *
     * @param user
     */
    @Override
    public void add(User user) {
        System.out.println("user服务添加用户");
        userMapper.insert(user);
        System.out.println("user服务添加用户成功");
    }


    /**
     * 修改
     *
     * @param user
     */
    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    /**
     * 删除
     *
     * @param username
     */
    @Override
    public void delete(String username) {
        userMapper.deleteByPrimaryKey(username);
    }


    /**
     * 条件查询
     *
     * @param searchMap
     * @return
     */
    @Override
    public List<User> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return userMapper.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<User> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<User>) userMapper.selectAll();
    }

    /**
     * 条件+分页查询
     *
     * @param searchMap 查询条件
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @Override
    public Page<User> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        return (Page<User>) userMapper.selectByExample(example);
    }

    @Override
    @Transactional
    public int updateUserPoints(Task task) {
        //获取任务数据
        Map info = JSON.parseObject(task.getRequestBody(), Map.class);
        String username = info.get("userName").toString();
        String orderId = info.get("orderId").toString();
        int point = (int) info.get("point");

        //判断当前订单是否操作过
        PointLog pointLog = pointLogMapper.findLogInfoByOrderId(orderId);
        if (pointLog != null) {
            return 0;
        }

        //将任务存入redis
        redisTemplate.boundValueOps(task.getId()).set("exist", 30, TimeUnit.SECONDS);

        //修改用户积分
        int result = userMapper.updateUserPoint(username, point);
        if (result <= 0) {
            return result;
        }

        //添加积分日志表记录
        pointLog = new PointLog();
        pointLog.setOrderId(orderId);
        pointLog.setPoint(point);
        pointLog.setUserId(username);
        result = pointLogMapper.insertSelective(pointLog);
        if (result <= 0) {
            return result;
        }

        //删除redis中的记录
        redisTemplate.delete(task.getId());

        return 1;
    }

    @Override
    @Transactional
    public void editHeadPic(String username, String headPic) {
        User user = userMapper.selectByPrimaryKey(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");  //这里可以不判断
        }
        user.setHeadPic(headPic);
        userMapper.updateByPrimaryKey(user);  //修改头像地址
    }

    @Override
    @Transactional
    public void editUserInfo(String username, Map<String, String> map) {
        User user = userMapper.selectByPrimaryKey(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");  //这里可以不判断
        }
        String nick = map.get("nick");
        //如果昵称不为空,设置昵称
        if (StringUtils.isNotBlank(nick)){
            user.setNickName(nick);
        }
        String gender = map.get("gender");
        //如果性别不为空,设置昵称
        if (StringUtils.isNotBlank(gender)){
            user.setSex(gender);
        }
        user.setSourceType("1");
        //编辑用户信息
        userMapper.updateByPrimaryKeySelective(user);

        //获取地址信息
        String province = map.get("province");
        String city = map.get("city");
        String area = map.get("area");
//        //获取省份id
//        Provinces provinces = new Provinces();
//        provinces.setProvince(province);
//        provinces = provincesMapper.selectOne(provinces);
//        String provinceid = provinces.getProvinceid();
//        //获取城市id
//        Cities cities = new Cities();
//        cities.setProvinceid(provinceid);
//        cities.setCity(city);
//        cities = citiesMapper.selectOne(cities);
//        String cityid = cities.getCityid();
//        //获取地区id
//        Areas areas = new Areas();
//        areas.setArea(area);
//        areas.setCityid(cityid);
//        areas = areasMapper.selectOne(areas);
//        String areaid = areas.getAreaid();
        //将id设置进地址信息中
        Address address = new Address();  //直接将地址信息存入表中,存的不是id
        address.setUsername(username);
        address.setProvinceid(province);
        address.setCityid(city);
        address.setAreaid(area);
        //添加地址信息
        addressMapper.insertSelective(address);
    }

    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> userCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";
            Integer count = userMapper.findMemberCountBeforeDate(date);
            userCount.add(count);
        }
        return userCount;
    }
    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 用户名
            if (searchMap.get("username") != null && !"".equals(searchMap.get("username"))) {
                criteria.andEqualTo("username", searchMap.get("username"));
            }
            // 密码，加密存储
            if (searchMap.get("password") != null && !"".equals(searchMap.get("password"))) {
                criteria.andEqualTo("password", searchMap.get("password"));
            }
            // 注册手机号
            if (searchMap.get("phone") != null && !"".equals(searchMap.get("phone"))) {
                criteria.andLike("phone", "%" + searchMap.get("phone") + "%");
            }
            // 注册邮箱
            if (searchMap.get("email") != null && !"".equals(searchMap.get("email"))) {
                criteria.andLike("email", "%" + searchMap.get("email") + "%");
            }
            // 会员来源：1:PC，2：H5，3：Android，4：IOS
            if (searchMap.get("sourceType") != null && !"".equals(searchMap.get("sourceType"))) {
                criteria.andEqualTo("sourceType", searchMap.get("sourceType"));
            }
            // 昵称
            if (searchMap.get("nickName") != null && !"".equals(searchMap.get("nickName"))) {
                criteria.andLike("nickName", "%" + searchMap.get("nickName") + "%");
            }
            // 真实姓名
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 使用状态（1正常 0非正常）
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            // 头像地址
            if (searchMap.get("headPic") != null && !"".equals(searchMap.get("headPic"))) {
                criteria.andLike("headPic", "%" + searchMap.get("headPic") + "%");
            }
            // QQ号码
            if (searchMap.get("qq") != null && !"".equals(searchMap.get("qq"))) {
                criteria.andLike("qq", "%" + searchMap.get("qq") + "%");
            }
            // 手机是否验证 （0否  1是）
            if (searchMap.get("isMobileCheck") != null && !"".equals(searchMap.get("isMobileCheck"))) {
                criteria.andEqualTo("isMobileCheck", searchMap.get("isMobileCheck"));
            }
            // 邮箱是否检测（0否  1是）
            if (searchMap.get("isEmailCheck") != null && !"".equals(searchMap.get("isEmailCheck"))) {
                criteria.andEqualTo("isEmailCheck", searchMap.get("isEmailCheck"));
            }
            // 性别，1男，0女
            if (searchMap.get("sex") != null && !"".equals(searchMap.get("sex"))) {
                criteria.andEqualTo("sex", searchMap.get("sex"));
            }

            // 会员等级
            if (searchMap.get("userLevel") != null) {
                criteria.andEqualTo("userLevel", searchMap.get("userLevel"));
            }
            // 积分
            if (searchMap.get("points") != null) {
                criteria.andEqualTo("points", searchMap.get("points"));
            }
            // 经验值
            if (searchMap.get("experienceValue") != null) {
                criteria.andEqualTo("experienceValue", searchMap.get("experienceValue"));
            }

        }
        return example;
    }

}
